#include "widget.h"
#include "ui_widget.h"
#include "QuestionItem.h"
#include <QDebug>
#include <QResizeEvent>
#include <QLabel>
#include <QPainter>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::resizeEvent(QResizeEvent *event)
{
    static long idx = 0;
    ++idx;
    qDebug()<<"Widget::resizeEvent was called,"<<QString::number(idx)<<","<<ui->listWidget->width();
}

void Widget::on_pushButton_clicked()
{
    static int i = 0;
    ++i;
    CQuestionItem *pQuestionItem = new CQuestionItem(ui->listWidget);
//    connect(pQuestionItem,SIGNAL(sigItemSizeChanged(int width,int height)),this,SLOT(on_itemsize_changed(int,int)));
    QListWidgetItem *pItem = new QListWidgetItem;

    QString str;
    if(i%5== 0)
        str = "围城读书笔记好词摘抄:囫囵吞枣,深不可测,如痴如狂,如傻如狂,怅然若失,怅然有失,人穷志短,人穷智短,垂头丧气,众叛亲离,离亲叛众";
    else if(i%5 == 1||i%5 == 2||i%5 == 3)
        str = "abchello,world.nice to meet you. glad to see you. byebye";
    else
        str = "围城读书笔记好词摘抄:囫囵吞枣,深不可测,如痴如狂,如傻如狂,怅然若失,怅然有失,人穷志短,人穷智短,垂头丧气,众叛亲离,离亲叛众围城读书笔记好词摘抄:囫囵吞枣,深不可测,如痴如狂,如傻如狂,怅然若失,怅然有失,人穷志短,人穷智短,垂头丧气,众叛亲离,离亲叛众";
    pQuestionItem->fillText(i,str);
    QLabel *label = new QLabel(nullptr);
    label->setFixedWidth(ui->listWidget->width());
    label->setText("围城读书笔记好词摘抄:囫囵吞枣,深不可测,如痴如狂,如傻如狂,怅然若失,怅然有失,人穷志短,人穷智短,垂头丧气,众叛亲离,离亲叛众围城读书笔记好词摘抄:囫囵吞枣,深不可测,如痴如狂,如傻如狂,怅然若失,怅然有失,人穷志短,人穷智短,垂头丧气,众叛亲离,离亲叛众");
    int hei = label->height();
    pItem->setSizeHint(QSize(ui->listWidget->width(),i%5 == 4?160:120)); //100:60|6:3
    ui->listWidget->addItem(pItem);
    ui->listWidget->setItemWidget(pItem, pQuestionItem);

//    pQuestionItem->fillText(i,"官方文档说");
//    pQuestionItem->fillText(i,"逝者如斯夫,臣一路行来，没有敌人,看到的只有朋友和师长");
    pQuestionItem->setHeadIcon(":/head.png");
}

void Widget::on_itemsize_changed(int width,int height)
{
    for (int i=0;i<ui->listWidget->count();++i) {
        ui->listWidget->item(i)->setSizeHint(QSize(ui->listWidget->width(),height));
    }
}
